from playwright.sync_api import sync_playwright
from playwright.sync_api import sync_playwright,Page


def get_token():
    """
    获取文件系统的token
    """
    with sync_playwright() as p:
        brower = p.chromium.launch(headless=False)
        page = brower.new_page()
        page.goto("http://aed.cnpdc.com")
        url = page.url
        if r"https://uap.cgnpc.com.cn/" in url:
            try:
                page.wait_for_selector('//*[@id="ifrOnceSearch"]', timeout=60000)

            except Exception as e:
                print("获取验证超时")
                return
        url = str(page.url)
        return url_to_token(url)


def url_to_token(url):
    if "token" in url:
        token = url.split("&")[-1]
        return token


def check_token(token):
    url = "http://aed.cnpdc.com/aed/Index/AEDPortal.aspx?status=new&" + token
    with sync_playwright() as p:
        brower = p.chromium.launch(headless=True)
        page = brower.new_page()
        page.goto(url)
        try:
            page.wait_for_selector('//*[@id="ifrOnceSearch"]', timeout=20000)
            return True

        except Exception as e:
            print("获取验证超时")
            return False



def select_option(page:Page,id:str,value:str):
    if id[0] != "#":
        id = f'#{id}'
    page.select_option(id,value)

def excute_js(page:Page,js_str:str):
    js_code = '()=>{return '+js_str+';}'
    page.evaluate(js_code)


def set_el_value(page:Page,el_id,value,value_name="value"):
    el = page.query_selector(el_id)
    if el:
        # el.set_attribute("value",review_depart)
        el.evaluate(f'node=>node.setAttribute("{value_name}","{value}")')



# # region 文件审查 添加
def add_review_doc(driver:Page, channel_code):
    print("开始添加文件")
    driver.get_by_role("link", name="文件清单").click()
    driver.get_by_text("新增文件").click()

    driver.frame_locator("#frmDialogaddfile").locator('#txtHJCode').fill(channel_code)  # 输入传递单号
    driver.frame_locator("#frmDialogaddfile").locator('#btnSearch').click()  
    driver.frame_locator("#frmDialogaddfile").locator('#ckboxs').click() 

    # 点击保存按钮
    txt = "保存"
    driver.locator( '//div[@class="dialog-button"]/a//span[text()="保存"]').click()
       
    alert_ = driver.get_by_text("确认")
    if alert_.count():
        alert_.click()


def add_attachment(driver:Page, att_file_str:str):
    if att_file_str:
        att_file_list = att_file_str.split(",")
        print(att_file_list)
        driver.get_by_text("附件信息").click()
        for file_doc in att_file_list:
            if file_doc:
                driver.locator('#sysFileUpLoad').click()
                # driver.wait_for_selector("#addFileXTWJ")
                # 添加系统文件
                driver.frame_locator("#frmDialogaddFileXTWJ").locator('#txtHJCode').fill(file_doc.strip())
                driver.frame_locator("#frmDialogaddFileXTWJ").locator('#btnSearch').click()
                driver.frame_locator("#frmDialogaddFileXTWJ").locator('//div[@class="datagrid-view2"]//div[@class="datagrid-header-check"]/input').click()

                # 保存文件
                btn_save_file_xpath = '//div[@id="addFileXTWJ"]/following-sibling::*//a[1]'
                driver.locator(btn_save_file_xpath).click()
        
        print("文件添加完成")

